﻿using ControllerManageServer.ActionModel;
using ControllerManageServer.ActionModel.Aioi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ControllerManageServer.LabelDevice.Aioi
{
    class BaseLabelDevice : ILabelDevice
    {
        private string _address;
        /// <summary>
        /// 设备地址
        /// </summary>
        public string Address
        {
            get { return _address; }
            set
            {
                if (value == null || value.Length < 4)
                {
                    throw new Exception("错误:爱鸥标签地址长度应该是4!");
                }
                if (value.Length >= 4)
                { _address = value.Substring(0, 4); }
            }
        }
        /// <summary>
        /// 字符编码,默认是ASCII码
        /// </summary>
        public Encoding DefaultEncoding = Encoding.ASCII;
        /// <summary>
        /// 显示长度,默认为5
        /// </summary>
        public int SeqLength = 5;

        /// <summary>
        /// 动作指令
        /// </summary>
        /// <param name="iModel">模式组合</param>
        /// <param name="iSeq">显示数据</param>
        /// <param name="iFnSeq">按下Confirm后显示数据</param>
        /// <returns></returns>
        public virtual byte[] Action(ActionModelAbstract iModel, string iSeq, string iFnSeq = null)
        {
            List<byte> result = new List<byte>();

            result.AddRange(DefaultEncoding.GetBytes("PP505"));// 命令
            result.AddRange(DefaultEncoding.GetBytes(string.IsNullOrEmpty(iFnSeq) ? "00" : "05"));// 指定Fn键按下后是否显示数据
            result.AddRange(DefaultEncoding.GetBytes("00"));// 区域号码
            result.AddRange(iModel.ModeArray());// 模式组合
            result.AddRange(DefaultEncoding.GetBytes(Address));// 地址
            if (iSeq == null) iSeq = "";
            result.AddRange(DefaultEncoding.GetBytes(SeqHandle(iSeq)));// 显示数据
            if (!string.IsNullOrEmpty(iFnSeq))
            {
                result.AddRange(DefaultEncoding.GetBytes(SeqHandle(iFnSeq)));// Fn键按下后显示的数据
            }

            return result.ToArray();
        }

        /// <summary>
        /// 显示序列的处理
        /// </summary>
        /// <param name="iSeq"></param>
        /// <returns></returns>
        protected string SeqHandle(string iSeq)
        {
            string temp = iSeq.PadLeft(SeqLength, ' ');
            return temp.Substring(0, SeqLength);
        }

        /// <summary>
        /// 取消动作
        /// </summary>
        /// <returns></returns>
        public byte[] Cancel()
        {
            string cmd = $"D{Address}";
            return DefaultEncoding.GetBytes(cmd);
        }

        /// <summary>
        /// 取消动作
        /// </summary>
        /// <returns></returns>
        public byte[] Cancel(string iLongAddress)
        {
            string cmd = $"D{iLongAddress}";
            return DefaultEncoding.GetBytes(cmd);
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>
        public byte[] Init()
        {
            return DefaultEncoding.GetBytes("Z");
        }

        /// <summary>
        /// 心跳机制
        /// </summary>
        /// <returns></returns>
        public byte[] Heartbeat()
        {
            return DefaultEncoding.GetBytes("I");
        }

        /// <summary>
        /// 标签状态
        /// </summary>
        /// <returns></returns>
        public byte[] LabelInfo()
        {
            string cmd = $"Az{Address}";
            return DefaultEncoding.GetBytes(cmd);
        }

        public virtual byte[] GetBatteryVoltage()
        {
            throw new NotImplementedException();
        }
    }
}
